<!--
 * @Author: your name
 * @Date: 2022-04-20 10:06:40
 * @LastEditTime: 2023-07-05 10:46:04
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \jk_one-stop_cloud_mobile_v1.0\src\views\apply\applyPay.vue
-->
<template>
  <div>
    <Navbar appTitle="申请采购" />
    <div class="applyPay-content wrap-apply" :style="{top: GLOBAL.currentEnvironment ? '0.1rem' : '0.52rem',height: GLOBAL.currentEnvironment ? '100vh' : 'calc(100vh - 1.12rem)',paddingBottom: GLOBAL.currentEnvironment ? '0.66rem' : ''}">
      <van-cell-group>
        <p class="label required FormTit"><span></span>使用日期</p>
         <!-- <van-field-select-picker label="" placeholder="请选择" v-model="buildForm.useDate" :columns="contractList" :option="{ label: 'contractName', value: 'id' }"
          @confirm="contractConfirm" @prePopShow="showContractDialog" /> -->
          
            <dateTime
                :supportClear="true"
                title="使用日期"
                type="date"
                 :value="buildForm.useDate"
                  @confirmTime="(v) => {
                    buildForm.useDate = v;
                  }
                "  
              />
        <div class="bottomLine"></div>
      </van-cell-group>
      <van-cell-group>
        <p class="label required FormTit"><span></span>采购事由</p>
        <van-field v-model="buildForm.reason" rows="2" autosize label="" type="textarea" maxlength="500" placeholder="请输入" show-word-limit />
        <div class="bottomLine"></div>
      </van-cell-group>
      
      <van-cell-group style='margin-top:0.1rem' v-for="(item,index) in selectedRows" :key="index"> 
          <div class="label otherText vcmiddle">
         <span>采购明细{{index +1}}</span>
         <img
          class="delimg"
          @click="deleteGoods(index)"
          src="@/assets/img/delete.png"
         
        />
          </div>
        <div class="bottomLine"></div>
       
        <p class="label required FormTit"><span></span>商品编号|商品名称</p>
        <div class="label-context">{{item.goodsCode}}|{{item.goodsName}}</div>
        <div class="bottomLine"></div><div class="bottomLine"></div>
        <p class="label required FormTit"><span></span>规格</p>
        <div class="label-context">{{item.spec}}</div>
        <div class="bottomLine"></div><div class="bottomLine"></div>
        <p class="label required FormTit"><span></span>本次采购数量<span v-if="item.basicUnitText">({{item.basicUnitText}})</span></p>
        <van-field type="number" maxlength="30" v-model="item.num" placeholder="请先输入"  @change="(item) => inputChange(item, index)" />
        <div class="bottomLine"></div><div class="bottomLine"></div>
      </van-cell-group>
      <div class="addbtn" @click="chooseGood"><van-icon class="vicon" name="plus" size="18" color="#1890ff" />选择采购商品</div>
      
       <van-cell-group style="margin-top: 0.12rem">
        <!-- <upload iconType="fj" @fileChange="fileChange" :fileIds="buildForm.attachment" /> -->
        <fileSelector
          iconType="fj"
          :title="'附件'"
          @fileChange="fileChange"
          :fileIds="buildForm.attachment"
        ></fileSelector>
      </van-cell-group>
      <div style="margin-top: 0.12rem">
        <Examine @submitProcess="submitProcess" :key="examKey" type="0"  id="ERP_PURCHASE_TASK_APPLY" ref="examine" />
        <div style="height: 0.26rem"></div>
      </div>
     <div class="search-bottoms">
        <span class="btnconfirm" @click="btnConfirm('finish')">提交</span>
      </div>
    
    </div>
    <choose-tag
      :contractId="contractId"
      ref="chooseTag"
      v-if="showChooseTag"
      @chooseTag="chooseTag"
      @cancelTag="cancelTag"
    ></choose-tag>
      
  </div>
</template>
<script>
import { erpPurchaseTaskApplyAdd} from "@/api/mobile.js";
import Navbar from "@/components/navBar/navBar_right";
import scanInput from "@/components/scanCode/scanInput";
import ContactPickers from "@/components/choiceuse/ContactPickers";
import upload from "@/components/upload/upload";
import VanFieldSelectPicker from "@/components/ActionSheet/VanFieldSelectPicker";
import VanFieldCheckbox from "@/components/ActionSheet/VanFieldCheckbox";
import { clearNoNum, accAdd, accSub } from "@/utils/util";
import Examine from "@/components/examine";
import DateTime from "@/components/form/dateTime";
import ChooseTag from "./chooseCgtag";
import fileSelector from "@/components/fileSelector.vue";
export default {
  name: "search",
  components: {
    ContactPickers,
    upload,
    VanFieldSelectPicker,
    VanFieldCheckbox,
    Navbar,
    Examine,
    ChooseTag,
    DateTime,
    fileSelector
  },
  data() {
    return {
      contractId:"",
      showChooseTag:false,
      taskId: "",
      orderId: "",
      notQualifiedReason: [],
      customerList:[],//客户 
      contractList:[],//销售合同
      selectedRows:[],
      buildForm: {
        useDate:"",
        reason: "",
        attachment:""
      },
      dictOptions: [], //不合格原因
      createdByName: "", //操作人
      columns: [],
      workOrderList: [], //完整的工单编号列表
      taskList: [], //工序列表
      operatorList: [], //操作人
      attachment: "", //附件
      subimtType: "",
      examKey: 1,
      formPage: "",
      currentLoginUser: {},
    };
  },
  computed: {},
  mounted() {},
  created() {
    // this.getInitData()
  },
  methods: {
    //
    confirmTime(){

    },
    cancelTag(){
      this.showChooseTag=false
    },
    deleteGoods(index){
      // this.selectedRows.findIndex(index)
       this.selectedRows.splice(index, 1);
    },
    inputChange(e, i) {
      let num = e.target.value
      this.selectedRows[i].num = num
      this.selectedRows.splice()
    },
    chooseGood(){
       this.showChooseTag=true
    },
    chooseTag(item){
      console.log(item)
     if(this.selectedRows.findIndex((element)=>(element.id ==item.id))!=-1){
           this.$toast("您已选择过该商品");
     }else{
         if(!item.num){
           item.num = ""
         }
          item.goodsId=item.id
          this.selectedRows.push(item)
          this.showChooseTag=false
     }
  
    },
    
    //页面加载请求数据
   
    showPeopleDialog(callback) {
       callback("show");
    },

 
    //
    fileChange(item){
      console.log(item)
      this.buildForm.attachment=item
  
    },
     // 提交
    btnConfirm() {
        
      if (!this.buildForm.useDate) {
        this.$toast("请选择使用日期");
        return;
      }
      if (!this.buildForm.reason) {
        this.$toast("请输入采购事由");
        return;
      }
      if (this.selectedRows==0) {
        this.$toast("请选择采购商品");
        return;
      }
      let isOver=false;
      this.selectedRows.forEach((item,index)=>{
        if(item.num==""){
           isOver=true
           return 
        }
      }) 
      if(isOver){
        this.$toast("请输入采购数量");
        return 
      }
      this.submitProcess();
    },
    submitProcess(e){
      if (e && e.nodeDtos) {
        // 获取到数据后写表单提交的方法
        this.handleSubmit(e);
      } else {
        // 调用子组件的方法获取数据
        this.$refs.examine.confirm();
      }
    },
    // 提交
    handleSubmit(processDataVo) {
      let params = {
        ...this.buildForm,
        type: "production",
        processDataVo: processDataVo,
        erpPurchaseTaskApplyItemDto: this.selectedRows
      };
      console.log("检查一下参数：", params);
      erpPurchaseTaskApplyAdd(params).then((res) => {
            if (res.code == 200) {
              this.$toast("申请采购成功！");
              this.$router.go(-1);
            } else {
              this.$toast(res.message)
            }
      })
    },
  },
};
</script>
<style lang="scss" scoped>
@import "@/assets/theme.scss";
.applyPay-content {
  //  padding-bottom: 0.5rem;
  position: fixed;
  top: 0.52rem;
  width: 100%;
  height: calc(100vh - 1.12rem);
  background: #f5f5f5;
  overflow: scroll;
  z-index: 99;
  .formgroup {
    background: #ffffff;
    // padding: 0 0.16rem;
    margin-top: 0.1rem;
    margin-bottom: 0.2rem;
    padding-top: 0.1rem;

    .title {
      width: 0.58rem;
      height: 0.14rem;
      font-size: 0.15rem;
      font-family: PingFang SC;
      font-weight: 400;
      color: #191f25;
      line-height: 0.05rem;
      margin-left: 0.16rem;
    }
  }
  .search-bottom {
    height: 0.5rem;
    width: 100%;
    background: #ffffff;
    // background: red;
    display: flex;
    justify-content: space-between;
    align-items: center;
    position: fixed;
    bottom: 0px;
    padding: 0.11rem;
    .btnconfirm {
      width: 100%;
      height: 0.39rem;
      background: #1890ff;
      border-radius: 0.05rem;
      text-align: center;
      line-height: 0.39rem;

        font-size: 0.16rem;
        font-family: PingFang SC;
        font-weight: 500;
        color: #FFFFFF;
    }
    .btncancel {
      width: 1.72rem;
      height: 0.39rem;
      background: #f6f6f6;
      border: 0.01px solid rgba(25, 31, 37, 0.12);
      border-radius: 0rem;
      text-align: center;
      line-height: 0.39rem;
    }
  }
}
.bottomLine {
  height: 0.01rem;
  background-color: #f0f0f0;
  margin: 0 0.16rem;
}
.bottomContent{
    padding:0.17rem;
    .num{
        color:orange;
    }
}
.addbtn{
    background-color: #ffffff;
    vertical-align: middle;
    padding: 0.1rem 0.17rem;
    text-align: center;
    // border: 1px solid red;
    // display: flex;
    margin-top: 0.1rem;
    font-size: 0.16rem;
    font-family: PingFang SC;
    font-weight: 400;
    color: #1890FF;
    .vicon{
        // display: inline-block;
         vertical-align: middle;
         position: relative;
         top: -0.01rem;
    }
}
.otherText {
  padding-bottom: 0.16rem;
  font-size: 0.16rem;
  font-weight: bold;
  color: #191f25;
   width: 100% ; 
}
.label-context{
    padding: 0.16rem;                                          
}
.vcmiddle{
 
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.delimg{
    width: 0.16rem;
    height: 0.16rem;
    float: left;
    // margin-right: 0rem;
   
}
.haveUnitBox {
  display: flex;
  justify-content: space-between;
  align-items: center;
  box-sizing: border-box;
  padding-right: 0.16rem;
  /deep/ .van-cell {
    padding-right: 0;
    width: 2.6rem;
  }
  /deep/ .van-field__control {
    max-width: 3rem;
  }
}
/deep/ .van-nav-bar {
  z-index: 99;
}

.search-bottoms {
  width: 100%;
  height: 0.5rem;
  background: #ffffff;
  display: flex;
  justify-content: space-between;
  align-items: center;
  position: fixed;
  bottom: 0px;
  padding: 0rem 0.11rem;
  box-sizing: border-box;
  font-size: 0.16rem;
  font-weight: 500;
  z-index: 9;
  .btnconfirm {
    flex: 1;
    background: #1890ff;
    box-sizing: border-box;
    height: 0.39rem;
    display: flex;
    justify-content: center;
    align-items: center;
    border-radius: 0.03rem;
    color: #ffffff;
  }
}
</style>
